<?php
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Server\ServerFactory;
use Hyperf\Utils\ApplicationContext;
use Psr\Http\Message\ServerRequestInterface;
use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
use Hyperf\Contract\ConfigInterface;
use Fyfyu\Admin\Constants\ErrCode;
use Fyfyu\Admin\Admin;
use Hyperf\Utils\Coroutine;
/**
 * 容器实例
 */
if (!function_exists('container')) {
    function container()
    {
        return ApplicationContext::getContainer();
    }
}
/**
 * 协程id
 * @var [type]
 */
if( ! function_exists('co_id') ){
    function co_id(){
        return Coroutine::id();
    }
}
/**
 * 配置类
 * @var [type]
 */
// if (!function_exists('config')) {
//     function config($key=false,$value=false)
//     {
//         $config = container()->get(ConfigInterface::class);
//         if( $key  && $value){
//             return $config->set($key,$value);
//         }
//         if( $key ){
//             return $config->get($key);
//         }
//         return $config;
//     }
// }

/**
 * redis 客户端实例
 */
if (!function_exists('redis')) {
    function redis()
    {
        return container()->get(Redis::class);
    }
}

/**
 * server 实例 基于 swoole server
 */
if (!function_exists('server')) {
    function server()
    {
        return container()->get(ServerFactory::class)->getServer()->getServer();
    }
}

/**
 * websocket frame 实例
 */
if (!function_exists('frame')) {
    function frame()
    {
        return container()->get(Frame::class);
    }
}

/**
 * websocket 实例
 */
if (!function_exists('websocket')) {
    function websocket()
    {
        return container()->get(WebSocketServer::class);
    }
}

/**
 * 缓存实例 简单的缓存
 */
if (!function_exists('cache')) {
    function cache()
    {
        return container()->get(Psr\SimpleCache\CacheInterface::class);
    }
}

/**
 * 控制台日志
 */
if (!function_exists('stdLog')) {
    function stdLog()
    {
        return container()->get(StdoutLoggerInterface::class);
    }
}

/**
 * 文件日志
 */
if (!function_exists('logger')) {
    function logger()
    {
        return container()->get(LoggerFactory::class)->make();
    }
}

/**
 *
 */
if (!function_exists('request')) {
    function request()
    {
        return container()->get(ServerRequestInterface::class);
    }
}

/**
 *
 */
if (!function_exists('response')) {
    function response()
    {
        return container()->get(ResponseInterface::class);
    }
}

if( ! function_exists('success') ){
    function success($data=false,$header=[]){
        $res = [
            'status' => 'success',
            'result' => ErrCode::getMessage(ErrCode::SUCCESS),
            'code' => ErrCode::SUCCESS,
        ];
        if( $data !== false ){
            $res['data'] = $data;
        }
        $resp = response()->json($res);
        return $resp;
    }
}
if( ! function_exists('error') ){
    function error($code=false,$msg='',$info=''){
        $data = [
            'status' => 'error',
        ];
        if( $code !== false ){
            $data['result'] = $msg | ErrCode::getMessage($code);
            $data['code'] = $code;
            $data['info'] = $info;
        }
        $resp = response()->json($data);
        return $resp;
    }
}
if( ! function_exists('admin') ){
    function admin($code=false,$msg='',$info=''){
        return container()->get(Admin::class);
    }
}
